iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 26
1
自我挑戰組

今年我想陪著 30 天系列 第 26

今年我想陪著 30 天之 26

  • 分享至 

  • xImage
  •  

804. Unique Morse Code Words

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.
For convenience, the full table for the 26 letters of the English alphabet is given below:
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cab" can be written as "-.-..--...", (which is the concatenation "-.-." + ".-" + "-..."). We'll call such a concatenation, the transformation of a word.
Return the number of different transformations among all words we have.

  • Example 1:
    Input: words = ["gin", "zen", "gig", "msg"]
    Output: 2
    Explanation:
    The transformation of each word is:
    "gin" -> "--...-."
    "zen" -> "--...-."
    "gig" -> "--...--."
    "msg" -> "--...--."

    There are 2 different transformations, "--...-." and "--...--.".

var uniqueMorseRepresentations = function(words) {
    code = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."];
    codeStore = [];
    for (let i = 0; i < words.length; i++){
        decode = "";
        word = words[i];
        for (let j = 0; j < word.length; j++){
            decode += code[(word[j].charCodeAt() - 97)];
        }
        if (codeStore.includes(decode) != true){
            codeStore.push(decode)
        }
    }
    
    return codeStore.length;
};

上一篇
今年我想陪著 30 天之 25
下一篇
今年我想陪著 30 天之 27
系列文
今年我想陪著 30 天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言